//package jl.security.filter;
//
//import io.micrometer.core.instrument.util.StringUtils;
//import jl.security.core.controller.CaptchaController;
//import jl.security.exception.VerifyCodeException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.web.authentication.AuthenticationFailureHandler;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * 图形验证码校验过滤器
// * OncePerRequestFilter: 所有请求之前被调用一次
// *
// * @author 江黎
// * @date 2022-02-08
// */
//@Component
//public class VerifyCodeValidateFilter extends OncePerRequestFilter {
//
//    @Autowired
//    private AuthenticationFailureHandler authenticationFailureHandler;
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        // 1. 如果是post方式 的登录请求，则校验输入的验证码是否正确
//        if ("/login".equals(request.getRequestURI()) && request.getMethod().equalsIgnoreCase("post")) {
//            try {
//                //校验验证码合法性
//                validate(request);
//            } catch (VerifyCodeException e) {
//                //将验证失败的抛出的异常交给自定义认证失败处理器处理异常
//                authenticationFailureHandler.onAuthenticationFailure(request, response, e);
//                return;
//            }
//        }
//        filterChain.doFilter(request, response);
//    }
//
//    private void validate(HttpServletRequest request) {
//        //先获取session中的验证码
//        String sessionImageCode = (String) request.getSession().getAttribute(CaptchaController.SESSION_VERIFY_CODE);
//        //获取用户输入的验证码
//        String inputCode = request.getParameter("code");
//        if (StringUtils.isBlank(inputCode)) {
//            throw new VerifyCodeException("验证码不能为空");
//        }
//        if (!inputCode.equalsIgnoreCase(sessionImageCode)) {
//            throw new VerifyCodeException("验证码输入错误");
//        }
//    }
//}